import numpy as np


class EnsembleClfs:
    def __init__(self, clf_list):
        self.clf_list = clf_list
        self.n_clfs = len(clf_list)
        self.trained_clfs = [None] * self.n_clfs
        self.trained_ids = []


    def fit(self, X, y, clf_id):
        clf = self.clf_list[clf_id]  # 选择使用哪个惩罚参数的分类器
        clf.fit(X, y)
        self.trained_clfs[clf_id] = clf
        self.trained_ids += [clf_id]

    def predict(self, X):
        n_trained = len(self.trained_clfs)
        pred_list = np.zeros((X.shape[0], n_trained))  # 行数为测试集的行数，列数为分类器的个数，用于综合多次预测的结果

        for i in self.trained_ids:
            clf = self.trained_clfs[i]

            y_pred = clf.predict_proba(X)[:, 1]  #返回预测属于某标签的概率
            pred_list[:, i] = y_pred

        return np.mean(pred_list, axis=1)
